﻿app.controller('polygonController', function PostsController($scope, $filter, PolygonFactory, $rootScope, uploadManager,$http) {
   
    $scope.loading = true;
    $scope.addMode = false;
    $scope.success = false;
   
  /*  $scope.isInvalid = function (field) {
        return $scope.form[field].$invalid && $scope.form[field].$dirty;
    };
    $scope.isValid = function (field) {
        return $scope.form[field].$valid && $scope.form[field].$dirty;
    };*/
    $scope.files = [];
   
    $scope.filesdone = [];
    
    $scope.percentage = 0;
    $scope.waitMessage = "טוען נא להמתין";

    $scope.closeDialog = function () {
        $scope.message = null;
    };

   
    $rootScope.$on('fileAdded', function (e, call) {
        $scope.files.push(call);
        $scope.$apply();
    });

    $rootScope.$on('uploadProgress', function (e, call) {
        $scope.percentage = call;
        $scope.$apply();
    });
    $scope.save = function () {
        $scope.waitMessage = "שומר ";
       // var Polyg = this.Polygon;
        PolygonFactory.updatePolygon($scope.Polygon).success(function (response) {
           
            if (response) {
              
                
                    $scope.message = response;
                    $scope.success = true;
                    $scope.Polygon = angular.copy($scope.Polygon);
                  
                    
                   
            }
            $scope.waitMessage = null;
        });
    };

 
  

    //listen for the file selected event
    $scope.$on("fileSelected", function (event, args) {
        $scope.$apply(function () {
            //add the file object to the scope's files collection
            $scope.files.push(args.file);
        });
    });

    //the save method
    $scope.upload = function () {
        var files = $("#file1").get(0).files;
        if (files.length > 0) {
            if (window.FormData !== undefined) {
                var data = new FormData();
                for (i = 0; i < files.length; i++) {
                    data.append("file" + i, files[i]);
                }
              
                $.ajax({
                    type: "POST",
                    url: "/hetz/api/fileupload",
                    contentType: false,
                    processData: false,
                    data: data,
                    success: function (results) {
                        $scope.Polygon.Attachment = "";
                        for (i = 0; i < results.length; i++) {
                           
                            //alert("Successfully uploaded " + results[i].Name + " (" + results[i].Size + " bytes)");
                          
                         
                            if (null != results[i]) {
                                $scope.Polygon.Attachment += results[i];
                                $scope.filesdone.push(results[i]);
                            }
                            $scope.$apply();
                        }
                       
                      
                       

                    }
                });
            } else {
                alert("This browser doesn't support HTML5 multiple file uploads!");
            }
        }
    };
   

   

    $scope.uploadold = function () {
       
        $scope.Polygon.Attachment = uploadManager.upload();
        $scope.files = [];
    };
    $scope.clear = function () {
        $scope.message = null;
        $scope.success = false;

    };
   


    //get all Customers
    PolygonFactory.getPolygon(getParameterByName("ID")).success(function (data) {
       
        $scope.Polygon = data;
        $scope.Polygon.StartDate = $filter('date')($scope.Polygon.StartDate, "dd/MM/yyyy");
        $scope.loading = false;
        $scope.waitMessage = null;
        $scope.filesdone = $scope.Polygon.Attachment.split("#")
       
    })
.error(function (data) {
    $scope.error = "An Error has occured while loading posts! " + data.ExceptionMessage;
    $scope.loading = false;
});

});